package com.xiaoke.accountsoft.activity;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhang on 2017/5/10.
 */

//创建基于BaseAdapter的子类
 public class PictureAdapter extends BaseAdapter {   //创建List泛型集合
    private LayoutInflater inflater;
    //创建List泛型集合
    private List<Picture> pictures;

//为类创建构造函数
public PictureAdapter(String[] titles, int[] images, Context context) {
        super();
        //初始化泛型集合对象
        pictures = new ArrayList<Picture>();
        //初始化LayoutInflater对象
        inflater = LayoutInflater.from(context);
        //遍历图像数组
        for (int i = 0; i < images.length; i++) {
        //使用标题和图像生成Picture对象
        Picture picture = new Picture(titles[i], images[i]);
        //将Picture对象添加到泛型集合中
        pictures.add(picture);
        }
        }

@Override
//获取泛型集合的长度
public int getCount() {
        //如果泛型集合不为空
        if (null != pictures) {
        //返回泛型长度
        return pictures.size();
        } else {
        //返回0
        return 0;
        }
        }

@Override
public Object getItem(int arg0) {
        //获取泛型集合指定索引处的项
        return pictures.get(arg0);
        }

@Override
public long getItemId(int arg0) {
        //返回泛型集合的索引
        return arg0;
        }

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
        //创建ViewHolder对象
        ViewHolder viewHolder;
        //判断图像标识是否为空
        if (arg1 == null) {
        //设置图像标识
        arg1 = inflater.inflate(R.layout.gvitem, null);
        //初始化ViewHolder对象
        viewHolder = new ViewHolder();
        //设置图像标题
        viewHolder.title = (TextView) arg1.findViewById(R.id.ItemTitle);
        //设置图像的二进制值
        viewHolder.image = (ImageView) arg1.findViewById(R.id.ItemImage);
        //设置提示
        arg1.setTag(viewHolder);
        } else {
        //设置提示
        viewHolder = (ViewHolder) arg1.getTag();
        }
        //设置图像标题
        viewHolder.title.setText(pictures.get(arg0).getTitle());
        //设置图像的二进制值
        viewHolder.image.setImageResource(pictures.get(arg0).getImageId());
        //返回图像标识
        return arg1;
        }
        }